package com.lanou.web.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanou.entity.TbItemParam;
import com.lanou.service.ItemParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by QuChao on 2018/11/8
 */
@Controller
public class ItemParamController {
    @Autowired
    private ItemParamService itemParamService;

    @ResponseBody
    @RequestMapping("/item/param/list")
    public Map<String, Object> list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                    @RequestParam(value = "rows", defaultValue = "30") Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        Page<Object> startPage = PageHelper.startPage(page, pageSize);
        List<Object> mapList = itemParamService.findAll();

        map.put("total", startPage.getTotal());
        map.put("rows", mapList);

        return map;
    }

    @ResponseBody
    @RequestMapping("/item/param/delete")
    public Map<String, Object> delete(String[] ids) {
        boolean result = itemParamService.deleteByIds(ids);
        Map<String, Object> map = new HashMap<>();
        if (result) {
            map.put("status", 200);
        } else {
            map.put("status", 400);
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/item/param/save/{id}")
    public Map<String, Object> save(TbItemParam tbItemParam, @PathVariable("id") Integer id) {
        return itemParamService.save(id, tbItemParam);
    }

    @ResponseBody
    @RequestMapping("/item/param/query/itemcatid/{id}")
    public Map<String, Object> query(@PathVariable("id") Integer id) {
        return itemParamService.findByItemCatId(id);
    }

    @ResponseBody
    @RequestMapping("/rest/item/param/item/query/{id}")
    public Map<String, Object> queryResult(@PathVariable("id") Long id) {
        return itemParamService.findByItemCatIdResultAll(id);
    }

}
